//
//  ToursimThirdVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "ToursimThirdVC.h"
#import "ToursimThirdModel.h"
#import "ToursimThirdCell.h"
#define Y 210.0f

@interface ToursimThirdVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

// 声明一个可变数组用来装model
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) ToursimThirdCell *viewCell;

@property (nonatomic, assign) CGFloat height;



@end

@implementation ToursimThirdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 给属性赋初值
    
    [self addUpData];
    [self addSubView];
    self.tableView.separatorStyle = NO;
    
    
}


// 加载视图
- (void)addSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KsWidth, KsHeigh ) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    // 添加刷新方法
    self.tableView.bounces = NO;
    [self.view addSubview:_tableView];
    self.navigationItem.title= @"景点详情";
   //
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toursimBarBackcolor"]];
    // 设置tabar的颜色
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemAction)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

// 设置按钮的登录和收藏的相关触发事件
- (void)rightItemAction
{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"dfasksoie";
    ToursimThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ToursimThirdCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    
    ToursimThirdModel *model = [self.dataArray firstObject];
    NSURL *urls = [NSURL URLWithString:self.imageString];
    [cell.topImageView sd_setImageWithURL:urls];
    if ([model.name length] == 0) {
        cell.titleLabel.text = @"亲,想尝尝吗?";
    } else {
    cell.titleLabel.text = model.name;
    }
    if ([model.recommended_reason isEqual:[NSNull null]]) {
        cell.recommended_reasonLabel.text =@"对此物的评论暂时还没有奥!";
    } else if ([model.recommended_reason length] == 0) {
        cell.recommended_reasonLabel.text =@"对此物的评论暂时还没有奥!";
    } else {
    cell.recommended_reasonLabel.text =  model.recommended_reason;
    }
    if ([model.descriptions length] == 0) {
        cell.descriptionsLabel.text = @"景点描述:  暂无对此物的描述";
    } else {
    cell.descriptionsLabel.text = [NSString stringWithFormat:@"景点描述:\n        %@", model.descriptions];
    }
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGRect frame = [cell.descriptionsLabel.text boundingRectWithSize:CGSizeMake(KsWidth - 10, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGRect newFrame = cell.descriptionsLabel.frame;
    newFrame.size.height = frame.size.height;
    cell.descriptionsLabel.frame = newFrame;
    // 设置下面的显示框的frame
    CGRect newFrame1 = cell.addressLabel.frame;
    newFrame1.origin.y = cell.descriptionsLabel.frame.origin.y + cell.descriptionsLabel.frame.size.height + 25;
    cell.addressLabel.frame = newFrame1;
    if ([model.address length] == 0) {
        cell.addressLabel.text = @"地址:  因地点不固定,所以暂时不提供线路介绍";
    } else {
    cell.addressLabel.text = [NSString stringWithFormat:@"地址:   %@", model.address];
    }
    // 独自设置一下到达线路的label的高度
    if ([model.arrival_type length] == 0) {
        cell.arrival_typeLabel.text = @"线路指导:  抱歉,暂时没有相关信息";
    } else {
    cell.arrival_typeLabel.text = [NSString stringWithFormat:@"线路指导:\n        %@", model.arrival_type];
    }
    CGRect frame2 = [cell.arrival_typeLabel.text boundingRectWithSize:CGSizeMake(KsWidth, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGRect newFrame2 = cell.arrival_typeLabel.frame;
     newFrame2.origin.y = cell.addressLabel.frame.origin.y +cell.addressLabel.frame.size.height + 5;
    newFrame2.size.height = frame2.size.height;
    cell.arrival_typeLabel.frame = newFrame2;
     

    // 设置开放时间的frame
    CGRect frame3 = cell.opening_timeLabel.frame;
    frame3.origin.y = cell.arrival_typeLabel.frame.origin.y + cell.arrival_typeLabel.frame.size.height + 5;
    cell.opening_timeLabel.frame = frame3;
    if ([model.opening_time length] == 0) {
        cell.opening_timeLabel.text = @"开放时间:   无时间限制";
    } else {
    cell.opening_timeLabel.text = [NSString stringWithFormat:@"开放时间:  %@", model.opening_time];
    }
// 设置票价的frame
    CGRect frame4 = cell.feeLabel.frame;
    frame4.origin.y = cell.opening_timeLabel.frame.origin.y + cell.opening_timeLabel.frame.size.height + 5;
    cell.feeLabel.frame = frame4;
    if ([model.fee length] == 0) {
        cell.feeLabel.text = @"票价:  暂无收费的相关信息!";
    } else {
     cell.feeLabel.text = [NSString stringWithFormat:@"票价:    %@", model.fee];
    }
    self.height = cell.feeLabel.bottom;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.height;
}

// 加载数据,首先进行数据的解析
- (void)addUpData
{
    [self.dataArray removeAllObjects];
    NSString *stringURl = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.types, self.idds];
    NSURL *url = [NSURL URLWithString:stringURl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 进行数据的解析
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
       // 开始博取数据
        ToursimThirdModel *model = [[ToursimThirdModel alloc] init];
        model.recommended_reason = dic[@"recommended_reason"];
        model.fee = dic[@"fee"];
        model.opening_time = dic[@"opening_time"];
        model.descriptions = dic[@"description"];
        model.arrival_type = dic[@"arrival_type"];
        model.address = dic[@"address"];
        model.name = dic[@"name"];
        [self.dataArray addObject:model];
        
        [self.tableView reloadData];
    }];
}

// 可变数组的懒加载
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
        return _dataArray;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
